package com.jason.campusinn.view.CreationClothing;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.yuandian.wanna.R;

public class CustomInnerCircleMenuLayout extends ViewGroup
{
    public int mRadius;
    /**
     * 该容器内child item的默认尺寸
     */
    private static final float RADIO_DEFAULT_CHILD_DIMENSION = 1 / 4f;
    /**
     * 菜单的中心child的默认尺寸
     */
    private float RADIO_DEFAULT_CENTERITEM_DIMENSION = 1 / 3f;
    /**
     * 该容器的内边距,无视padding属性，如需边距请用该变量
     */
    private static final float RADIO_PADDING_LAYOUT = 1 / 12f;

    /**
     * 当每秒移动角度达到该值时，认为是快速移动
     */
    private static final int FLINGABLE_VALUE = 300;

    /**
     * 如果移动角度达到该值，则屏蔽点击
     */
    private static final int NOCLICK_VALUE = 3;

    /**
     * 当每秒移动角度达到该值时，认为是快速移动
     */
    private int mFlingableValue = FLINGABLE_VALUE;
    /**
     * 该容器的内边距,无视padding属性，如需边距请用该变量
     */
    private float mPadding;

    /**
     * 菜单项的文本
     */

    //TODO: Core Data
    /**
     * 菜单项的图标
     */
    private ArrayList<Bitmap> mItemImgs;//image resource list
    private ArrayList<Double> mMenuAngles;//imageview angle
    private ArrayList<Integer> mMenuIndex;//imageview resouce id
    private ArrayList<Boolean> mMenuVisible;//imageview visible

    /**
     * 菜单的个数
     */
    private int mMenuItemCount;

    /**
     * 检测按下到抬起时旋转的角度
     */
    private float mTmpAngle;
    /**
     * 检测按下到抬起时使用的时间
     */
    private long mDownTime;

    /**
     * 判断是否正在自动滚动
     */
    private boolean isFling;

    private int mMenuItemLayoutId = R.layout.depth_customization_circle_menu_item;

    public CustomInnerCircleMenuLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        mItemImgs = new ArrayList<Bitmap>();
        mMenuIndex = new ArrayList<Integer>();

        mMenuAngles = new ArrayList<Double>();
        mMenuVisible = new ArrayList<Boolean>();

        // 无视padding
        setPadding(0, 0, 0, 0);
    }

    // 圆心坐标
    private int mPointX = 0, mPointY = 0;
    /**
     * 设置布局的宽高，并策略menu item宽高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int resWidth = 0;
        int resHeight = 0;

        /**
         * 根据传入的参数，分别获取测量模式和测量值
         */
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        /**
         * 如果宽或者高的测量模式非精确值
         */
        if (widthMode != MeasureSpec.EXACTLY
                || heightMode != MeasureSpec.EXACTLY)
        {
            // 主要设置为背景图的高度
            resWidth = getSuggestedMinimumWidth();
            // 如果未设置背景图片，则设置为屏幕宽高的默认值
            resWidth = resWidth == 0 ? getDefaultWidth() : resWidth;

            resHeight = getSuggestedMinimumHeight();
            // 如果未设置背景图片，则设置为屏幕宽高的默认值
            resHeight = resHeight == 0 ? getDefaultWidth() : resHeight;
        } else
        {
            // 如果都设置为精确值，则直接取小值；
            resWidth = resHeight = Math.min(width, height);
        }

//		setMeasuredDimension(resWidth, resHeight);
//
//		// 获得半径
//		mRadius = Math.max(getMeasuredWidth(), getMeasuredHeight());
//
//		mPointX = this.getMeasuredWidth()/2;
//		mPointY = this.getMeasuredHeight()/2;

        setMeasuredDimension(width,height);
        mRadius = Math.min(width, height);
        mPointX = width/2;
        mPointY = height/2;
//        Log.e("onMeasure", String.valueOf(mRadius)+":"+
//                String.valueOf(mPointX)+":"+
//                String.valueOf(mPointY));

        // menu item数量
        final int count = getChildCount();
        // menu item尺寸
        int childSize = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION);
        // menu item测量模式
        int childMode = MeasureSpec.EXACTLY;

        // 迭代测量
        for (int i = 0; i < count; i++)
        {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE)
            {
                continue;
            }

            // 计算menu item的尺寸；以及和设置好的模式，去对item进行测量
            int makeMeasureSpec = -1;

            if (child.getId() == R.id.id_circle_menu_item_center)
            {
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(
                        (int) (mRadius * RADIO_DEFAULT_CENTERITEM_DIMENSION),
                        childMode);
            } else
            {
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(childSize,
                        childMode);
            }
            child.measure(makeMeasureSpec, makeMeasureSpec);
        }

        //mPadding = RADIO_PADDING_LAYOUT * mRadius;
        mPadding = 0;

    }

    /**
     * MenuItem的点击事件接口
     *
     * @author zhy
     *
     */
    public interface OnMenuItemClickListener
    {
        void itemClick(View view, int pos);

        void itemCenterClick(View view);

        void itemDragDone(int pos);
    }

    /**
     * MenuItem的点击事件接口
     */
    private OnMenuItemClickListener mOnMenuItemClickListener;

    /**
     * 设置MenuItem的点击事件接口
     *
     * @param mOnMenuItemClickListener
     */
    public void setOnMenuItemClickListener(
            OnMenuItemClickListener mOnMenuItemClickListener)
    {
        this.mOnMenuItemClickListener = mOnMenuItemClickListener;
    }

    public int m_cur_selindex = -1;
    private void updateImageifNeed(final int child_id, final int index)
    {

        if(index < mItemImgs.size())
        {

            updateBorderColor(m_cur_selindex);


            final View child = getChildAt(child_id);

            //Log.e("updateImageifNeed", String.valueOf(index));

            ImageView iv = (ImageView) child
                    .findViewById(R.id.id_circle_menu_item_image);

            if (iv != null)
            {
                iv.setVisibility(View.VISIBLE);
                iv.setImageBitmap(mItemImgs.get(index));
                iv.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {

                        if (mOnMenuItemClickListener != null)
                        {
                            m_cur_selindex = mMenuIndex.get(child_id);
                            updateBorderColor(m_cur_selindex);
                            mOnMenuItemClickListener.itemClick(v, index);
                        }
                    }
                });
            }

        }
    }

    private int mYDeta = 0;
    private void DrawMenuItem(int i)
    {
        int left, top;
        int layoutRadius = mRadius;
        final View child = getChildAt(i);

        if(!mMenuVisible.get(i))
        {
            child.setVisibility(View.INVISIBLE);
        } else {
            child.setVisibility(View.VISIBLE);
            updateImageifNeed(i, mMenuIndex.get(i));
        }

        // menu item 的尺寸
        int cWidth = (int) (layoutRadius * RADIO_DEFAULT_CHILD_DIMENSION);

        // 计算，中心点到menu item中心的距离
        float tmp = layoutRadius / 2f - cWidth / 2 - mPadding;

        // tmp cosa 即menu item中心点的横坐标
        left = layoutRadius
                / 2
                + (int) Math.round(tmp
                * Math.cos(Math.toRadians(mMenuAngles.get(i))) - 1 / 2f
                * cWidth);
        // tmp sina 即menu item的纵坐标
        top = layoutRadius
                / 2
                + mYDeta
                + (int) Math.round(tmp
                * Math.sin(Math.toRadians(mMenuAngles.get(i))) - 1 / 2f
                * cWidth);

        child.layout(left, top, left + cWidth, top + cWidth);

    }
    /**
     * 设置menu item的位置
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        int layoutRadius = mRadius;
//        Log.e("onLayout", String.valueOf(layoutRadius));
        // Laying out the child views
        final int childCount = getChildCount();

        // menu item 的尺寸
        int cWidth = (int) (layoutRadius * RADIO_DEFAULT_CHILD_DIMENSION);

        // 根据menu item的个数，计算角度
        float angleDelay = 360 / 8;

        if(mCur!=-1)
        {
            final View child = getChildAt(mCur);

            child.layout((int)(mDragX - cWidth/2), (int)(mDragY - cWidth/2),
                    (int)(mDragX + cWidth/2), (int)(mDragY + cWidth/2));
        } else {
            // 遍历去设置menuitem的位置
            for (int i = 0; i < childCount; i++)
            {
                DrawMenuItem(i);
            }
        }
    }

    private int mCur = -1;
    private boolean mIsDragging = false;
    private int getInCircle(float x, float y) {

        final int childCount = getChildCount();
        int menuRadius = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION)/2;

        for (int i = 0; i < childCount; i++)
        {
            final View child = getChildAt(i);

            if (child.getId() == R.id.id_circle_menu_item_center)
                continue;

            if (child.getVisibility() == GONE)
            {
                continue;
            }

            int mx = (int)child.getX()+menuRadius/2;
            int my = (int)child.getY()+menuRadius/2;

            if (((x - mx) * (x - mx) + (y - my) * (y - my)) < menuRadius * menuRadius) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 根据触摸的位置，计算角度
     *
     * @param xTouch
     * @param yTouch
     * @return
     */
    private float getAngle(float xTouch, float yTouch)
    {
        double x = xTouch - (mRadius / 2d);
        double y = yTouch - (mRadius / 2d) - mYDeta;

        float degree = (float) (Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI);

        return degree;
    }


    private boolean isTragIn(float x, float y)
    {
        boolean rtn = false;
//
//        Log.e("(Math.abs(x-mPointX)", String.valueOf((Math.abs(x-mPointX))));
//        Log.e("mRadius/2", String.valueOf(mRadius/2));


        if((Math.abs(x-mPointX)<mRadius/4)&&
                (Math.abs(y-mPointY)<mRadius/4))
        {
            rtn = true;
        }

        return rtn;
    }

    /**
     * 计算某点的角度
     *
     * @param x
     * @param y
     * @return
     */
    private int computeCurrentAngle(float x, float y) {
        float distance = (float) Math
                .sqrt(((x - mPointX) * (x - mPointX) + (y - mPointY)
                        * (y - mPointY)));
        int degree = (int) (Math.acos((x - mPointX) / distance) * 180 / Math.PI);
        if (y < mPointY) {
            degree = -degree;
        }

//        Log.d("RoundSpinView", "x:" + x + ",y:" + y + ",degree:" + degree);
        return degree;
    }

    /**
     * 记录上一次的x，y坐标
     */
    private float mLastX;
    private float mLastY;

    private float mDragX;
    private float mDragY;
    /**
     * 自动滚动的Runnable
     */
    private AutoFlingRunnable mFlingRunnable;
    private double startAngle;
    @Override
    public boolean dispatchTouchEvent(MotionEvent event)
    {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:

                mLastX = x;
                mLastY = y;
                mDownTime = System.currentTimeMillis();
                mTmpAngle = 0;

                // 如果当前已经在快速滚动
                if (isFling)
                {
                    // 移除快速滚动的回调
                    removeCallbacks(mFlingRunnable);
                    isFling = false;
                    return true;
                }

                mCur = getInCircle(x, y);

                if(mCur!=-1)
                {
                    mIsDragging = true;
//                    Log.e("ACTION_DOWN", "you selected item is:"+String.valueOf(mCur));
                } else {
                    startAngle = computeCurrentAngle(x, y);
                }

                break;
            case MotionEvent.ACTION_MOVE:

                if(mCur!=-1){
                    mDragX = x;
                    mDragY = y;
                    // 重新布局
//				requestLayout();
                } else {
//				/**
//				 * 获得开始的角度
//				 */
//				float start = getAngle(mLastX, mLastY);
//				/**
//				 * 获得当前的角度
//				 */
//				float end = getAngle(x, y);
//
//				// 如果是一、四象限，则直接end-start，角度值都是正值
//				if (getQuadrant(x, y) == 1 || getQuadrant(x, y) == 4)
//				{
//					mTmpAngle += end - start;
//					rotateButtons(end - start);
//				} else
//				// 二、三象限，色角度值是付值
//				{
//					mTmpAngle += start - end;
//					rotateButtons(start - end);
//				}

                    double currentAngle = computeCurrentAngle(x, y);
                    if(currentAngle - startAngle>0)
                    {
                        rotateButtons(10);
                        mTmpAngle += 10;
                    } else {
                        if(currentAngle - startAngle<0)
                        {
                            rotateButtons(-10);
                            mTmpAngle += -10;
                        }
                    }

                    startAngle = currentAngle;
                    mLastX = x;
                    mLastY = y;
                }
                break;
            case MotionEvent.ACTION_UP:

                mIsDragging = false;
                if(mCur!=-1)
                {
                    // 重新布局

//				if(isTragIn(x,y))
//				{
//					mOnMenuItemClickListener.itemDragDone(mMenuIndex.get(mCur));
//				}
//
//				requestLayout();
                } else {
                    // 计算，每秒移动的角度
                    float anglePerSecond = mTmpAngle * 1000
                            / (System.currentTimeMillis() - mDownTime);

                    // 如果达到该值认为是快速移动
                    if (Math.abs(anglePerSecond) > mFlingableValue && !isFling)
                    {
                        // post一个任务，去自动滚动
                        post(mFlingRunnable = new AutoFlingRunnable(anglePerSecond));

                        return true;
                    }

                    // 如果当前旋转角度超过NOCLICK_VALUE屏蔽点击
                    if (Math.abs(mTmpAngle) > NOCLICK_VALUE)
                    {
                        return true;
                    }
                }

                mCur = -1;
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 主要为了action_down时，返回true
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        return true;
    }



    /**
     * 根据当前位置计算象限
     *
     * @param x
     * @param y
     * @return
     */
    private int getQuadrant(float x, float y)
    {
        int tmpX = (int) (x - mRadius / 2);
        int tmpY = (int) (y - mRadius / 2) - mYDeta;
//		int tmpX = (int) (x - mPointX );
//		int tmpY = (int) (y - mPointY );
        if (tmpX >= 0)
        {
            return tmpY >= 0 ? 4 : 1;
        } else
        {
            return tmpY >= 0 ? 3 : 2;
        }

    }

    /**
     * 设置菜单条目的图标和文本
     *
     * @param resIds
     */
    public void setMenuItemIconsAndTexts(ArrayList<Bitmap> resIds, ArrayList<String> texts)
    {
        mItemImgs = resIds;

        int cWidth = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION);
//		mYDeta = mRadius/2 - cWidth/2;

        // 参数检查
        if (resIds == null && texts == null)
        {
            throw new IllegalArgumentException("菜单项文本和图片至少设置其一");
        }

        // 初始化mMenuCount
        mMenuItemCount = 8;
        if(mItemImgs.size()<mMenuItemCount)
        {
            mMenuItemCount = mItemImgs.size();
        }

        addMenuItems();
    }

    /**
     * 设置MenuItem的布局文件，必须在setMenuItemIconsAndTexts之前调用
     *
     * @param mMenuItemLayoutId
     */
    public void setMenuItemLayoutId(int mMenuItemLayoutId)
    {
        this.mMenuItemLayoutId = mMenuItemLayoutId;
    }

    public void removeAllData()
    {
        if(mItemImgs!=null)
        {
            mItemImgs.clear();
        }

        if(mMenuIndex!=null)
        {
            mMenuIndex.clear();
        }

        if(mMenuAngles!=null)
        {
            mMenuAngles.clear();
        }

        if(mMenuVisible!=null)
        {
            mMenuVisible.clear();
        }
    }
    /**
     * 添加菜单项
     */
    private void addMenuItems()
    {
        LayoutInflater mInflater = LayoutInflater.from(getContext());
        /**
         * 根据用户设置的参数，初始化view
         */

        int count = 8;

        for (int i = 0; i < count; i++)
        {
            mMenuAngles.add((double) (270.0f + 45.0f - i*45.0f));
            mMenuVisible.add(false);

            if(mMenuAngles.get(i)>=180&&mMenuAngles.get(i)<=270&&i<mItemImgs.size())
            {
                mMenuIndex.add(0);
            } else {
                mMenuIndex.add(i);
            }

            final int j = i;
            View view = mInflater.inflate(mMenuItemLayoutId, this, false);
            ImageView iv = (ImageView) view
                    .findViewById(R.id.id_circle_menu_item_image);
//			iv.setBorderWidth(10);
//			iv.setBorderColor(0xffffffff);

            if (iv != null)
            {
                if(i<mMenuItemCount){
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageBitmap(mItemImgs.get(i));
                    iv.setOnClickListener(new OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {

                            if (mOnMenuItemClickListener != null)
                            {
//								//updateBorderColor(j);
//								Log.e("addMenuItems", String.valueOf(j));
//								mOnMenuItemClickListener.itemClick(v, j);
//								m_cur_selindex = mMenuIndex.get(j);
//								updateBorderColor(m_cur_selindex);
//								mOnMenuItemClickListener.itemClick(v, mItemImgs.get(j));
                            }
                        }
                    });
                } else {
                    iv.setVisibility(View.INVISIBLE);
                    iv.setImageBitmap(mItemImgs.get(0));
                }
            }

            // 添加view到容器中
            addView(view);

        }

        if(mItemImgs.size()<6)
        {
            autoRotation(400);
        } else
        {
            if(mItemImgs.size()>8)
            {
                autoRotation(1000);
            } else {
                autoRotation(800);
            }
        }
    }

    private void updateBorderColor(int index)
    {
        final int childCount = getChildCount();

        //View child = getChildAt(id);

        for(int i=0; i<childCount; i++)
        {
            if(mMenuIndex.get(i)==index)
            {
//				CircleImageView iv = (CircleImageView) child.findViewById(R.id.id_circle_menu_item_image);
//				iv.setBorderColor(0xffff0000);
                View child = getChildAt(i);
                ImageView iv = (ImageView) child
                        .findViewById(R.id.id_circle_menu_item_image);
//				iv.setBorderColor(0xffff0000);
            } else {
                View child = getChildAt(i);
                ImageView iv = (ImageView) child
                        .findViewById(R.id.id_circle_menu_item_image);
//				iv.setBorderColor(0xffffffff);
            }
        }

//		for (int j = 0; j < childCount; j++)
//		{
//			final View child = getChildAt(j);
//			if(j==index)
//			{
//				CircleImageView iv = (CircleImageView) child
//						.findViewById(R.id.id_circle_menu_item_image);
//				iv.setBorderColor(0xffff0000);
//			} else {
//				CircleImageView iv = (CircleImageView) child
//						.findViewById(R.id.id_circle_menu_item_image);
//				iv.setBorderColor(0xffffffff);
//			}
//		}
    }

    /**
     * 如果每秒旋转角度到达该值，则认为是自动滚动
     *
     * @param mFlingableValue
     */
    public void setFlingableValue(int mFlingableValue)
    {
        this.mFlingableValue = mFlingableValue;
    }

    /**
     * 设置内边距的比例
     *
     * @param mPadding
     */
    public void setPadding(float mPadding)
    {
        this.mPadding = mPadding;
    }

    /**
     * 获得默认该layout的尺寸
     *
     * @return
     */
    public int getDefaultWidth()
    {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return Math.min(outMetrics.widthPixels, outMetrics.heightPixels);
    }

    private void autoRotation(int degree)
    {
        post(mFlingRunnable = new AutoFlingRunnable(degree));
    }
    /**
     * 自动滚动的任务
     *
     * @author zhy
     *
     */
    private class AutoFlingRunnable implements Runnable
    {

        private float angelPerSecond;

        public AutoFlingRunnable(float velocity)
        {
            this.angelPerSecond = velocity;
        }

        public void run()
        {
            // 如果小于20,则停止
            if ((int) Math.abs(angelPerSecond) < 20)
            {
                isFling = false;
                return;
            }
            isFling = true;
            // 不断改变mStartAngle，让其滚动，/30为了避免滚动太快
            rotateButtons(angelPerSecond / 28);
            // 逐渐减小这个值
            angelPerSecond /= 1.0666F;
            postDelayed(this, 30);
            // 重新布局
            //requestLayout();
        }
    }



    public int m_top_in=0;
    public int m_left_in=-1;
    public boolean top_in_end = false;
    public boolean left_in_end= false;
    /*
     * 旋转菜单按钮
     */
    private int m_key_rotation_top = 0;
    private int m_key_rotation_left = 0;
    private int m_StartAngle = 330;
    private void rotateButtons(double degree) {

        if(mItemImgs==null)
        {
            return;
        }

        if(mItemImgs.size()>=6)
        {

            if(top_in_end)
            {

                if(degree<0)
                {

                    if(m_key_rotation_top++<5)
                    {
                        return;
                    } else {
                        m_key_rotation_top = 0;
//                        Log.e("rotateButtons-top", String.valueOf(degree));
                        top_in_end = false;
                    }

                } else {
                    top_in_end = true;
                    return;
                }
            }

            if(left_in_end)
            {
                if(degree>0)
                {
                    if(m_key_rotation_left++<5)
                    {
                        return;
                    } else {
                        m_key_rotation_left = 0;
//                        Log.e("rotateButtons-left", String.valueOf(degree));
                        left_in_end = false;
                    }
                } else {
                    left_in_end = true;
                    return;
                }
            }

            for (int i = 0; i < mMenuAngles.size(); i++) {

                mMenuAngles.set(i, mMenuAngles.get(i)+degree);

                if (mMenuAngles.get(i) < 0) {
                    mMenuAngles.set(i, mMenuAngles.get(i)+360);
                }else if(mMenuAngles.get(i) >=360){
                    mMenuAngles.set(i, mMenuAngles.get(i)-360);
                }


                if((mMenuAngles.get(i)>m_StartAngle||(mMenuAngles.get(i)>=0&&mMenuAngles.get(i)<180)))
                {
                    if(degree>0)//从右向左划
                    {
//                        //>300
//                        Log.e("rotateButtons", "m_top_in:mItemImgs.size()["+String.valueOf(m_top_in)+":"+
//                                String.valueOf(mItemImgs.size())+"]");
                        //if(mMenuIndex.get(i)==0&&mMenuAngles.get(i)>270){
                        if(mMenuIndex.get(i)==0&&mMenuAngles.get(i)>m_StartAngle){
                            if(m_top_in<mItemImgs.size())
                            {
                                if(!mMenuVisible.get(i)){
                                    mMenuIndex.set(i, m_top_in);
                                    mMenuVisible.set(i, true);
                                    m_top_in++;
                                }
                            } else {
                                top_in_end = true;
                            }

                        }

                    }else{
                        //<180

                        if(mMenuIndex.get(i)==0&&mMenuAngles.get(i)<180){
                            if(m_left_in<=0)
                            {
                                left_in_end = true;
                            } else {
                                if(!mMenuVisible.get(i))
                                {
                                    m_left_in--;
                                    mMenuIndex.set(i, m_left_in);
                                    mMenuVisible.set(i, true);

                                }
                            }
                        }

                    }
                }
                else {
                    if(degree>0)//从右向左划
                    {

                        if(mMenuIndex.get(i)==0){
                            if(mMenuVisible.get(i))
                            {
                                m_left_in = 1;
                            }
                        } else {

                            if(mMenuIndex.get(i)==-1)
                            {
                                m_left_in = 0;
                            } else {
                                m_left_in = mMenuIndex.get(i) + 1;
                            }
                        }


                    } else {
                        if(mMenuIndex.get(i)==0){

                        } else {
                            m_top_in = mMenuIndex.get(i) ;
                        }
                    }
                    mMenuIndex.set(i, 0);
                    mMenuVisible.set(i, false);
                }

            }

        } else {
            if(mItemImgs.size()<6)
            {
                left_in_end = true;
                if(top_in_end)
                {
                    if(degree<0)
                    {
                        //top_in_end = false;
                    } else {

                        return;
                    }
                }

                if(left_in_end)
                {
                    if(degree>0)
                    {
                    } else {
                        return;
                    }
                }

                for (int i = 0; i < mMenuAngles.size(); i++) {

                    mMenuAngles.set(i, mMenuAngles.get(i)+degree);

                    if (mMenuAngles.get(i) < 0) {
                        mMenuAngles.set(i, mMenuAngles.get(i)+360);
                    }else if(mMenuAngles.get(i) >=360){
                        mMenuAngles.set(i, mMenuAngles.get(i)-360);
                    }


                    if((mMenuAngles.get(i)>m_StartAngle||(mMenuAngles.get(i)>=0&&mMenuAngles.get(i)<180)))
                    {
                        if(degree>0)//从右向左划
                        {
                            //>300
                            if(mMenuIndex.get(i)==0){
                                if(m_top_in<mItemImgs.size())
                                {
                                    if(!mMenuVisible.get(i)){
                                        mMenuIndex.set(i, m_top_in);
                                        mMenuVisible.set(i, true);
                                        m_top_in++;
                                    }
                                } else {
                                    top_in_end = true;

                                }

                            }

                        }else{
                            //<180

                            if(mMenuIndex.get(i)==0&&mMenuAngles.get(i)<180){
                                if(m_left_in<=0)
                                {
                                    left_in_end = true;
                                } else {
                                    if(!mMenuVisible.get(i))
                                    {
                                        m_left_in--;
                                        mMenuIndex.set(i, m_left_in);
                                        //mMenuVisible.set(i, true);

                                    }
                                }
                            }

                        }
                    }
                    else {
                        if(degree>0)//从右向左划
                        {

                            if(mMenuIndex.get(i)==0){
                                if(mMenuVisible.get(i))
                                {
                                    m_left_in = 1;
                                }
                            } else {

                                if(mMenuIndex.get(i)==-1)
                                {
                                    m_left_in = 0;
                                } else {
                                    m_left_in = mMenuIndex.get(i) + 1;
                                }
                            }


                        } else {
                            if(mMenuIndex.get(i)==0){

                            } else {
                                m_top_in = mMenuIndex.get(i) ;
                            }
                        }
                        mMenuIndex.set(i, 0);
                        mMenuVisible.set(i, false);
                    }

                }


            }

        }
        requestLayout();
    }

    public void setYDeta( int ydeta ){
        mYDeta = ydeta;
    }
}
